//
//  turn.c
//  Testat_5
//
#include "turn.h"
#include "printCard.h"
#include "cardStack.h"
#include "stdio.h"
#include "stdlib.h"



/***************************************************************************************
 FUNCTION    :  turn
 PURPOSE     :  Checks for any special cards, 
				prints open card and cards of player, 
				asks for and executes turn.

 INPUT       :  - Pointer to the card stack of the specific computer player
                - Pointer to the card stack
                - Pointer to the played stack
				- Pointer to executed card (for special cards)
				- Pointer to colorWish (for Jack)

 OUTPUT      :  - TURN_COMPLETE = Control value to signal a successfully performed move

 written by Tobias Stein
 ***************************************************************************************/
int turn(int (*playerStack)[NR_CARDS], int (*cardStack)[NR_CARDS],int (*playedCards)[NR_CARDS],int* executedCard, int* colorWish){
    int openCard =peekTopCardOfStack(playedCards);
	int k,i,t;
    int cardType = openCard % NR_TYPES;
    
    if(*colorWish!=NO_COLORWISH)
    {
        printf("\nYou have to fulfil the wish for ");
        printColor(*colorWish);
    }
    
    if(cardType == ACE && *executedCard != openCard)
    {
		printf("\nOpen card:\t");
		printCard(openCard);
        printf("\n>>> Seems like you have to skip this round...\n\n");
        *executedCard = openCard;
        return TURN_COMPLETE;
        
    }

    if(cardType == SEVEN && *executedCard != openCard)
    {
		printf("\nOpen card:\t");
		printCard(openCard);
        printf("\n>>> You have to draw %d cards!\n",NR_CARDS_DRAWN_WHEN_SEVEN);
        for (int i= 0; i<NR_CARDS_DRAWN_WHEN_SEVEN; i++)
        {
            
             printf("You draw: ");
             printCard(addCardToStack(playerStack, removeTopCardFromStack(cardStack)));
             putchar('\n');
           
            
        }
         *executedCard = openCard;
        putchar('\n');
		sortPlayersHand(playerStack);
		return TURN_COMPLETE;
    }
    
	k= numberOfCardsInStack(playerStack);
	i=0;
	t=0;
	//printing open card
	printf("\nOpen card:\t");
	printCard(openCard);
	printf("\n\nYour cards:\n");
	//printing hand cards
	printCardStack(playerStack);
	//asking for turn
	printf("\n\n[1 to %d]-put card, [0]-take card, [-1]-end game\nYour turn: ",k);
	do{
		scanf("%d",&i);
		if(i<-1||i>k)
		printf("False insert!\n");
	}while(i<-1||i>k);
	t=i;
	//take card
	if(t==0)
    {
        printf("\nYou draw: \t");
    printCard(addCardToStack(playerStack,removeTopCardFromStack(cardStack)));
    printf("\n\n");
	sortPlayersHand(playerStack);
    }
	//end game
	else if(t==-1)
        return EXIT_PROGRAM;
	//play card
	else{
        if(!validMove(openCard, (*playerStack)[t-1],*colorWish))
        {
            printf("\nPlaying: \t");
            printCard((*playerStack)[t-1]);
            printf(" is not a valid move. Try again.\n\n");
            return REPEAT_TURN;
        }
        // Valid Move -> Play Card
        printf("\nYou play: \t");
        *executedCard = openCard;
        
        if(printCard(addCardToStack(playedCards, removeCardAtPositionFromStack(playerStack, t-1)))%NR_TYPES == JACK)
        {
            printf("\n\nWish for a color:\n");
            for(int i= 0; i<NR_COLOURS; i++)
            {
                printf("%2d: \t",i+1);
                printColor(i);
                putchar('\n');
                
            }
            while(1)
            {
                printf("Please enter a color wish: ");
                int c;
                scanf("%d",&c);
                c--;
                if(c>=0 && c<NR_COLOURS)
                {
                    *colorWish = c;
                    break;
                }
            }
        } else {
			*colorWish=NO_COLORWISH;
		}
        printf("\n\n");
		
	}
    
   
     return TURN_COMPLETE;
}

/***************************************************************************************
 FUNCTION    :  computerPlayerTurn
 PURPOSE     :  Checks a computer player's hand for the first valid card to play and
                plays it, otherwise takes a card from stack.

 INPUT       :  - Pointer to the card stack of the specific computer player
                - Pointer to the card stack
                - Pointer to the open card

 OUTPUT      :  - TURN_COMPLETE = Control value to signal a successfully performed move

 written by Leander Cordes :D
 ***************************************************************************************/
int computerPlayerTurn(int (*playerStack)[NR_CARDS], int (*cardStack)[NR_CARDS], int (*playedCards)[NR_CARDS],int* executedCard, int* colorWish)
    {
    int openCard = peekTopCardOfStack(playedCards);
    int cardType = openCard % NR_TYPES;
    
    if(*colorWish!=NO_COLORWISH)
    {
        printf("\nPlayer has to fulfil the wish for ");
        printColor(*colorWish);
    }
    
    if(cardType == ACE && *executedCard != openCard)
    {
        printf("\n>> Seems like I have to skip this round...\n\n");
        *executedCard = openCard;
        return TURN_COMPLETE;

    }
    
    if(cardType == SEVEN && *executedCard != openCard)
        {
            printf("\nPlayer has to draw %d cards!\n",NR_CARDS_DRAWN_WHEN_SEVEN);
			printf(">> F###!");
            for (int i= 0; i<NR_CARDS_DRAWN_WHEN_SEVEN; i++)
            {
                    addCardToStack(playerStack, removeTopCardFromStack(cardStack));
                
            }
            *executedCard = openCard;

            putchar('\n');
			return TURN_COMPLETE;
        }
    
            int cardToPlay = NO_CARD;
            cardToPlay = findValidMove(playerStack, openCard, colorWish);
            if (cardToPlay == NO_CARD)
            {
                printf("\n>> This game sucks... I draw a card from the stack...\n\n");
                addCardToStack(playerStack,removeTopCardFromStack(cardStack));
            }
            else
            {
                printf("\n>> I play: \t");
                printCard((*playerStack)[cardToPlay]);
                printf("\n\n");
                if((addCardToStack(playedCards,removeCardAtPositionFromStack(playerStack, cardToPlay))%NR_TYPES)==JACK)
                {
                    printf(">> I wish for  ");
                    *colorWish = printColor(getDominantColorOfstack(playerStack));

                    putchar('\n');
                    putchar('\n');
                }
                else
                {
                    *colorWish= NO_COLORWISH;
                }
            }
        
         
    
    
	return TURN_COMPLETE;
}

